package wang.hu.bos.web.action;

import javax.ws.rs.core.MediaType;

import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import wang.hu.bos.domain.base.Area;
import wang.hu.bos.domain.take_delivery.Order;
import wang.hu.crm.domain.Customer;

@Controller
@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
public class OrderAction extends BaseAction<Order> {

	private String sendAreaInfo; // 寄件地址
	private String recAreaInfo; // 收件地址

	public void setSendAreaInfo(String sendAreaInfo) {
		this.sendAreaInfo = sendAreaInfo;
	}

	public void setRecAreaInfo(String recAreaInfo) {
		this.recAreaInfo = recAreaInfo;
	}

	@Action(value = "order_save", results = {
			@Result(name = "success", location = "index.html", type = "redirect"),
			@Result(name = "login", location = "login.html", type = "redirect") })
	public String saveOrder() {
		// 必须登录之后才能完成下单,
		Customer customer = (Customer) ServletActionContext.getRequest()
				.getSession().getAttribute("customer");
		if (customer == null) {
			return LOGIN;
		}
		model.setCustomer_id(customer.getId());

		String[] send = sendAreaInfo.split("/");
		Area sendArea = new Area();
		sendArea.setProvince(send[0]);
		sendArea.setCity(send[1]);
		sendArea.setDistrict(send[2]);

		String[] rec = recAreaInfo.split("/");
		Area recArea = new Area();
		recArea.setProvince(rec[0]);
		recArea.setCity(rec[1]);
		recArea.setDistrict(rec[2]);
		model.setSendArea(sendArea);
		model.setRecArea(recArea);

		WebClient
				.create("http://localhost:8080/bos_management/services/orderservice/saveorder")
				.type(MediaType.APPLICATION_JSON).post(model);
		return SUCCESS;
	}
}
